package com.coder.action;

import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;

import net.sf.json.JSONObject;

import com.coder.service.WaveService;
import com.coder.zexception.ServerErrorException;
import com.opensymphony.xwork2.ActionSupport;

public class WaveAction extends ActionSupport  implements SessionAware{

	private static final long serialVersionUID = 1L;

	private WaveService waveService;
	protected JSONObject jsonObject = new JSONObject();
	
	private int waveVal;
	private float threshold;
	
    //Struts2中Map类型的session
    private Map<String, Object> session;
    private String message; 
	private static final Logger LOGGER = Logger.getLogger(WaveAction.class);
	
	public String updateWaveThreshold(){
		LOGGER.info("更新波长报警阈值："+waveVal+": "+threshold);
		
		try{
			waveService.updateWaveThreshold(waveVal,threshold);
		}catch(Exception e){
			e.printStackTrace();
			jsonObject.put("success", false);
			return ERROR;
		}
		jsonObject.put("success", true);
		return SUCCESS;
	}
	
	/**根据一个波长来获取其对应的阈值*/
	public String getThresholdByWave(){
		
		float waveThd = 0;
		
		if(waveVal>710 || waveVal<200 || waveVal%2!=0){
			LOGGER.error("波长不在200-710范围内，或者输入了奇数，不能获取对应阈值");
			(new ServerErrorException("波长不在200-710范围内，或者输入了奇数，不能获取对应阈值")).printStackTrace();
			return ERROR;
		}
		
		waveThd = waveService.getThresholdByWave(waveVal);
		jsonObject.put("threshold", waveThd);
		
		return SUCCESS;
	}
	
	public JSONObject getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(JSONObject jsonObject) {
		this.jsonObject = jsonObject;
	}


	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public WaveService getWaveService() {
		return waveService;
	}

	public void setWaveService(WaveService waveService) {
		this.waveService = waveService;
	}

	public int getWaveVal() {
		return waveVal;
	}

	public void setWaveVal(int waveVal) {
		this.waveVal = waveVal;
	}

	public float getThreshold() {
		return threshold;
	}

	public void setThreshold(float threshold) {
		this.threshold = threshold;
	}

}
